package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author nzmxfan
 * @create 2021-09-03-20:11
 */
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    CustomerDao customerDao;

    @Override
    public Map<String, Object> list(Integer page, Integer rows, String customerName) {

        Map<String, Object> map = new HashMap<>();

        List<Customer> list = customerDao.list((page - 1) * rows, rows, customerName);

        int total = customerDao.total(customerName);

        map.put("rows", list);
        map.put("total", total);
        return map;
    }

    @Override
    public ServiceVO<Customer> saveOrUpdate(Customer customer) {
        Integer customerId = customer.getCustomerId();
        ServiceVO<Customer> vo;
        if (customerId == null) {
            // 添加
            vo = save(customer);
        } else {
            // 更新
            vo = update(customer);
        }
        return vo;
    }

    // 有外键
    @Override
    public ServiceVO<Customer> deleteById(String[] strings) {

        customerDao.deleteById(strings);
        return new ServiceVO(100, "请求成功", null);
    }

    private ServiceVO<Customer> update(Customer customer) {
        customerDao.update(customer);
        return new ServiceVO(100, "请求成功", null);
    }

    private ServiceVO<Customer> save(Customer customer) {

        customerDao.save(customer);
        return new ServiceVO(100, "请求成功", null);

    }
}
